package labox.innovation.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Noctarius
 */

public final class L2Properties extends Properties {
	private static final long serialVersionUID = 1L;

	private static Logger _log = LoggerFactory.getLogger(L2Properties.class.getName());

	public L2Properties() {
	}

	public L2Properties(String name) throws IOException {
		load(new FileInputStream(name));
	}

	public L2Properties(File file) throws IOException {
		load(new FileInputStream(file));
	}

	public L2Properties(InputStream inStream) throws IOException {
		load(inStream);
	}

	public L2Properties(Reader reader) throws IOException {
		load(reader);
	}

	public void load(String name) throws IOException {
		load(new FileInputStream(name));
	}

	public void load(File file) throws IOException {
		load(new FileInputStream(file));
	}

	@Override
	public void load(InputStream inStream) throws IOException {
		try {
			super.load(inStream);
		} finally {
			inStream.close();
		}
	}
	
	public void load(InputStream inStream,String charset) throws IOException {
		try {
			super.load(new InputStreamReader(inStream,charset));
		} finally {
			inStream.close();
		}
	}

	@Override
	public void load(Reader reader) throws IOException {
		try {
			super.load(reader);
		} finally {
			reader.close();
		}
	}

	@Override
	public String getProperty(String key) {
		String property = super.getProperty(key);

		if (property == null) {
			_log.info("L2Properties: Missing property for key - " + key);

			return null;
		}

		return property.trim();
	}

	@Override
	public String getProperty(String key, String defaultValue) {
		String property = super.getProperty(key, defaultValue);

		if (property == null) {
			_log.warn("L2Properties: Missing defaultValue for key - " + key);
			return null;
		}

		return property.trim();
	}
}
